package com.linh.trade.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import javax.swing.JOptionPane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.linh.trade.Constants.DATE_FORMAT;

;
public class MarketDataFromFile extends MarketData {
	private static final Logger LOG = LoggerFactory.getLogger(MarketDataFromFile.class);

	private String dataFile;

	

	public MarketDataFromFile(String dataFile) {
		super();
		this.dataFile = dataFile;
	}



	public List<Point> sourceData() {
		List<Point> points = new ArrayList<Point>();
		try {
			Scanner scanner = new Scanner(new File(dataFile));

			int id = 0;
			while (scanner.hasNext()) {
				String nextLine = scanner.nextLine();
				if (nextLine.startsWith("RateDateTime")) {
					continue;
				}
				String[] tokens = nextLine.split(",");
				try {
					String dateString = tokens[0];
					long nano = Long.parseLong(dateString.split("\\.")[1]);

					Date date = DATE_FORMAT.parse(dateString);
					// check
					nano = date.getTime() * 1000000 + nano;
					double rate = Double.parseDouble(tokens[1]);
					Point p = new Point(date, rate, nano, id);
					id++;
					points.add(p);
				} catch (Exception ex) {
					LOG.warn(ex.getMessage());
				}
			}

			scanner.close();

		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "Error", ex.getMessage(),
					JOptionPane.ERROR_MESSAGE);
			LOG.error("Error",ex);
		}
		return points;

	}

}
